<?php
/**
 *  Ajax 请求处理部分
 *
 *  Authors: 小照
 *  Mailbox: 2453162656@qq.com
 *
 */
include_once ('state.php');
$row = $DB->get_row("select * FROM " . DBQZ . "_user where uid ='".$userrow['uid']."' limit 1"); //获取用户信息

$my = isset($_POST['my'])?$_POST['my']:$_GET['my'];
switch ($my) {
	case 'set': # 更改系统配置
		$logobt 		= $_POST['logobt'];
		$name 			= $_POST['name'];
		$kfqq 			= $_POST['kfqq'];
		$qlurl 			= $_POST['qlurl'];
		$check 			= $_POST['check'];
		$check_time 	= $_POST['check_time'];
		$regcode 		= $_POST['regcode'];
		$activecode 	= $_POST['activecode'];
		$logincode 		= $_POST['logincode'];
		$reglimit 		= $_POST['reglimit'];
		$cnzz 			= $_POST['cnzz'];
		saveSetting('logobt',$logobt);
		saveSetting('name',$name);
		saveSetting('kfqq',$kfqq);
		saveSetting('qlurl',$qlurl);
		saveSetting('check',$check);
		saveSetting('regcode',$regcode);
		saveSetting('activecode',$activecode);
		saveSetting('logincode',$logincode);
		saveSetting('reglimit',$reglimit);
		saveSetting('cnzz',$cnzz);
		addlog($row['user'].' 修改了网站配置信息');
		$arr = array(
			'code' => 1,
			'msg' => '修改完成'
		);
		echo json_encode($arr); die;
	break;
	
	case 'infoedit': # 更改账号资料
		$uid 	= $_POST['uid'];
		$pwd 	= $_POST['pwd'];
		$mail 	= $_POST['mail'];
		$qq 	= $_POST['qq'];
		if(!$row_edit=$DB->get_row("select * from `". DBQZ ."_user` where `uid`={$uid}")){
			$arr = array(
				'code' => 0,
				'msg'  => '账号不存在'
			);
		}elseif($pwd==''){
			$pwd 	= $row['pwd'];
		}else{
			$pwd 	= md5($pwd);
		}
		$sql = $DB->query("UPDATE `". DBQZ ."_user` SET `pwd`='{$pwd}', `mail`='{$mail}', `qq`='{$qq}' where `uid`={$row_edit['uid']}");
		if($sql){
			addlog('管理员：'.$row['user'].' 修改了 '.$row_edit['user'].' 账号资料');
			$arr = array(
				'code' => 1,
				'msg' => '修改成功'
			);
		}else{
			$arr = array(
				'code' => 0,
				'msg' => '修改失败'
			);
		}
		echo json_encode($arr); die;
	break;
	
	case 'addlinks': # 添加友链
		$name 		= $_POST['name'];
		$url 		= $_POST['url'];
		$remarks 	= $_POST['remarks'];
		$date	 	= date("Y-m-d H:i:s");
		if($name=='' || $url==''){
			$arr = array(
				'code' => 0,
				'msg' => '必填选项不能留空'
			);
		}elseif(strstr($url,'http')==false){
			$arr = array(
				'code' => 0,
				'msg' => '请输入正确链接'
			);
		}elseif($DB->get_row("SELECT * from `". DBQZ ."_friendly` where `name`='{$name}'")){
			$arr = array(
				'code' => 0,
				'msg' => '友链已存在'
			);
		}elseif($DB->query("INSERT INTO `". DBQZ ."_friendly` (`name`,`url`,`remarks`,`date`) values ('{$name}','{$url}','{$remarks}','{$date}')")){
			addlog('管理员：'.$row['user'].' 添加了一条友链');
			$arr = array(
				'code' => 1,
				'msg' => '添加成功'
			);
		}else{
			$arr = array(
				'code' => 0,
				'msg' => '添加失败'
			);
		}
		echo json_encode($arr); die;
	break;
	
	case 'edityl': # 修改友情链接
		$id 		= intval($_POST['id']);
		$name 		= $_POST['name'];
		$url 		= $_POST['url'];
		$remarks 	= $_POST['remarks'];
		if($name=='' || $url==''){
			$arr = array(
				'code' => 0,
				'msg' => '必填选项不能留空'
			);
		}elseif(strstr($url,'http')==false){
			$arr = array(
				'code' => 0,
				'msg' => '请输入正确链接'
			);
		}elseif($DB->query("UPDATE `". DBQZ ."_friendly` set `name`='{$name}', `url`='{$url}', `remarks`='{$remarks}' where `id`={$id}")){
			addlog('管理员：'.$row['user'].' 修改了 '.$name.'[id:'.$id.'] 友链');
			$arr = array(
				'code' => 1,
				'msg' => '修改成功'
			);
		}else{
			$arr = array(
				'code' => 0,
				'msg' => '修改失败'
			);
		}
		echo json_encode($arr); die;
	break;
	
	case 'delyl': # 删除友情链接
		$id = intval($_POST['id']);
		if(!$id || $id==0){
			$arr = array(
				'code' => 0,
				'msg' => '参数无效'
			);
		}elseif(!$rowdel=$DB->get_row("SELECT * FROM `". DBQZ ."_friendly` where `id`={$id}")){
			$arr = array(
				'code' => 0,
				'msg' => '友链不存在'
			);
		}elseif($DB->query("DELETE FROM `". DBQZ ."_friendly` where `id`={$rowdel['id']}")){
			addlog('管理员：'.$row['user'].' 删除了一条友链');
			$arr = array(
				'code' => 1,
				'msg' => '删除成功'
			);
		}else{
			$arr = array(
				'code' => 0,
				'msg' => '删除失败'
			);
		}
		echo json_encode($arr); die;
	break;
	
	case 'userlist': # 获取用户列表
		$page 		= is_numeric($_GET['page']) ? $_GET['page'] : '1';
		$pagein 	= $page + 8;
		$pagesize 	= is_numeric($_GET['limit']) ? $_GET['limit'] : '10';
		$start 		= ($page - 1) * $pagesize;
		if(isset($_GET['search'])){
		$content 	= $_GET['search'];
			$pages 	= 'seach';
			$sql_Mysql = $DB->query("select * from ". DBQZ ."_user where qq='{$content}' or uid like'%{$content}%' or user like'%{$content}%' order by (case when qq='{$content}' then 8 else 0 end)+(case when user like '%{$content}%' then 3 else 0 end)+(case when user like '%{$content}%' then 3 else 0 end) desc limit 20");
		}else{
			$pages    = ceil($DB->count("select count(uid) as count from " . DBQZ . "_user where 1=1"));
			$sql_Mysql = $DB->query("select * from " . DBQZ . "_user  order by uid desc limit $start,$pagesize");
		}

		$i = 0;
		while ($row_Mysql = $DB->fetch($sql_Mysql)) {
			$obj[$i]['uid'] 	 = $row_Mysql['uid'];
			$obj[$i]['user'] 	 = $row_Mysql['user'];
			$obj[$i]['qq'] 		 = $row_Mysql['qq'];
			$obj[$i]['regtime']  = $row_Mysql['regtime'];
			$obj[$i]['lasttime'] = $row_Mysql['lasttime'].' / '.$row_Mysql['lastip'];
			$obj[$i]['caozuo'] 	 = '<a href="editinfo.php?uid='.$row_Mysql['uid'].'" class="layui-btn layui-btn-xs layui-btn-normal">编辑</a>
			<button type="button" class="layui-btn layui-btn-xs layui-btn-danger" onclick="deluser('.$row_Mysql['uid'].')">删除</button>';
			$i=$i+1;
		}
		$arr = array(
			'code' => 0,
			'msg' => '',
			'count' => $pages,
			'data' => $obj
		);
		echo json_encode($arr); die;
	break;
	
	case 'deluser': # 删除账号
		$uid = intval($_POST['uid']);
		if($uid==0 || $uid==1){
			$arr = array(
				'code' => 0,
				'msg'  => 'uid无效或是管理员无法删除'
			);
		}elseif(!$del_row=$DB->get_row("select * from `". DBQZ ."_user` where `uid`={$uid}")){
			$arr = array(
				'code' => 0,
				'msg'  => '账号不存在'
			);
		}elseif($DB->query("DELETE FROM `". DBQZ ."_user` where `uid`={$del_row['uid']}")){
			addlog('管理员：'.$row['user'].' 删除了一个账号');
			$arr = array(
				'code' => 1,
				'msg'  => '删除成功'
			);
		}else{
			$arr = array(
				'code' => 0,
				'msg'  => '删除失败'
			);
		}
		echo json_encode($arr); die;
	break;
	
	case 'loglist': # 操作日记
		$page 		= is_numeric($_GET['page']) ? $_GET['page'] : '1';
		$pagesize 	= is_numeric($_GET['limit']) ? $_GET['limit'] : '10';
		$start 		= ($page - 1) * $pagesize;
		$pages 		= ceil($DB->count("select count(id) as count from " . DBQZ . "_log  where 1=1"));
		$sql_Mysql  = $DB->query("select * from " . DBQZ . "_log  order by id desc limit $start,$pagesize");
		$i = 0;
		while ($row_Mysql = $DB->fetch($sql_Mysql)) {
			$obj[$i]['id'] 		= $row_Mysql['id'];
			$obj[$i]['ip'] 		= $row_Mysql['ip'];
			$obj[$i]['content'] = $row_Mysql['content'];
			$obj[$i]['date'] 	= $row_Mysql['date'];
			$i=$i+1;
		}
		$arr = array(
			'code' => 0,
			'msg' => '',
			'count' => $pages,
			'data' => $obj
		);
		echo json_encode($arr); die;
	break;
	
	case 'dellog': # 清空日记
		$DB->query("truncate table ". DBQZ ."_log");
		addlog('管理员：'.$row['user'].' 清空了操作日记');
		$arr = array(
			'code' => 1,
			'msg' => '清空成功'
		);
		echo json_encode($arr); die;
	break;
	
	case 'friendlylist': # 友链列表
		$content 	= defense($_POST['content']);
		$page 		= is_numeric($_GET['page']) ? $_GET['page'] : '1';
		$pagesize 	= is_numeric($_GET['limit']) ? $_GET['limit'] : '10';
		$start 		= ($page - 1) * $pagesize;
		if($content){
			//通过关键字搜索
			$pages ='seach'; 
			$sql_Mysql = $DB->query("select * from " . DBQZ . "_friendly where concat(`name`,`url`,`remarks`) like '%{$content}%' order by id desc limit 20");
		}else{
			$pages = ceil($DB->count("select count(id) as count from " . DBQZ . "_friendly  where 1=1") / $pagesize);
			$sql_Mysql = $DB->query("select * from " . DBQZ . "_friendly  order by id desc limit $start,$pagesize");
		}
		$i = 0;
		while ($row_Mysql = $DB->fetch($sql_Mysql)) {
			$obj[$i]['id'] 			= $row_Mysql['id'];
			$obj[$i]['name'] 		= $row_Mysql['name'];
			$obj[$i]['url'] 		= '<a href="'.$row_Mysql['url'].'" target="_blank" style="color:#1e9fff;">点击跳转</a>';
			$obj[$i]['remarks'] 	= $row_Mysql['remarks'];
			$obj[$i]['date'] 		= $row_Mysql['date'];
			$onclick				= "openedit('".$row_Mysql['id']."','".$row_Mysql['name']."','".$row_Mysql['url']."','".$row_Mysql['remarks']."')";
			$obj[$i]['caozuo'] 		= '<button type="button" class="layui-btn layui-btn-xs layui-btn-normal" onclick="'.$onclick.'">修改</button>
			<button type="button" class="layui-btn layui-btn-danger layui-btn-xs" onclick="delyl('.$row_Mysql['id'].')">删除</button>';
			$i=$i+1;
		}
		$arr = array(
			'code' => 0,
			'msg' => '',
			'count' => $pages,
			'data' => $obj
		);
		echo json_encode($arr); die;
	break;
	
	case 'emailset': # 发信邮件设置
		$mail_type 		= intval($_POST['mail_type']);
		$mail_stmp 		= defense($_POST['mail_stmp']);
		$mail_port 		= defense($_POST['mail_port']);
		$mail_name 		= defense($_POST['mail_name']);
		$mail_pwd 		= defense($_POST['mail_pwd']);
		saveSetting('mail_type',$mail_type);
		saveSetting('mail_stmp',$mail_stmp);
		saveSetting('mail_port',$mail_port);
		saveSetting('mail_name',$mail_name);
		saveSetting('mail_pwd',$mail_pwd);
		addlog($row['user'].' 修改了发信邮件配置');
		$arr = array(
			'code' => 1,
			'msg' => '修改完成'
		);
		echo json_encode($arr); die;
	break;
	
	
default: 
	# code...
break;
}